package com.fr.report.script.function;

import com.fr.base.core.DateUtils;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import java.util.Date;
import java.util.GregorianCalendar;

public class DATEINWEEK extends NormalFunction
{
  private int weekday;

  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 2)
      return Primitive.ERROR_NAME;
    Object localObject1 = paramArrayOfObject[0];
    Object localObject2 = paramArrayOfObject[1];
    if (!(localObject2 instanceof Number))
      return localObject1;
    int i = ((Number)localObject2).intValue();
    i = (i < 0) ? i + 8 : i;
    Date localDate = null;
    if (localObject1 instanceof Date)
      localDate = (Date)localObject1;
    else
      localDate = DateUtils.object2Date(localObject1, false);
    if (localDate == null)
      return Primitive.ERROR_NAME;
    GregorianCalendar localGregorianCalendar = new GregorianCalendar();
    localGregorianCalendar.setTime(localDate);
    int j = localGregorianCalendar.get(1);
    int k = localGregorianCalendar.get(2);
    int l = localGregorianCalendar.get(5);
    if (localGregorianCalendar.get(7) == 1)
      this.weekday = 6;
    else
      this.weekday = (localGregorianCalendar.get(7) - 1);
    if (i == -1)
      return DateUtils.createDate(j, k + 1, l + 6 - this.weekday);
    if (i == 0)
      return DateUtils.createDate(j, k + 1, l);
    return DateUtils.createDate(j, k + 1, l + i - this.weekday);
  }

  public Function.Type getType()
  {
    return Function.DATETIME;
  }

  public String getCN()
  {
    return "DATEINWEEK(date, number):函数返回在某一个星期当中第几天的日期。\n示例：\ndateInWeek(\"2008-08-28\", 2)等于2008-08-26。\ndateInWeek(\"2008-08-28\", -1)等于2008-08-31。\n如果最后一个参数为-1，返回该日期所在星期的最后一天\n";
  }

  public String getEN()
  {
    return "DATEINWEEK(date, number):return the date of one day in a week.\nExample：\ndateInWeek(\"2008-08-28\", 2)=2008-08-26。\ndateInWeek(\"2008-08-28\", -1)=2008-08-31。";
  }
}